package com.catazy.mvvmlib.ext.lifecycle

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import java.util.Stack

/**
 * 作者: Tao
 * 日期: 2024/1/24
 * 时间: 17:45
 * 描述: Activity管理器
 * 主要作用: 用来管理所有的Activity
 */
object ActivityManager {

    /**
     * activity管理栈
     */
    private val activityStack by lazy { Stack<Activity>() }

    /**
     * 添加Activity进栈
     * @param activity Activity
     */
    fun addActivityToStack(activity: Activity) {
        activityStack.push(activity)
    }

    /**
     * 弹出栈内指定Activity 不finish
     * @param activity Activity
     */
    fun popActivityToStack(activity: Activity) {
        if (!activityStack.empty()) {
            activityStack.forEach {
                if (it == activity) {
                    activityStack.remove(activity)
                    return
                }
            }
        }
    }

    /**
     * 返回到上一个 Activity 并结束当前 Activity
     */
    fun backToPreviousActivity() {
        if (!activityStack.empty()) {
            val activity = activityStack.pop()
            if (!activity.isFinishing) {
                activity.finish()
            }
        }
    }

    /**
     * 根据类名判断是否是当前的Activity
     * @param cls Class<*>
     * @return Boolean
     */
    fun isCurrentActivity(cls: Class<*>): Boolean {
        val currentActivity = getCurrentActivity()
        return if (currentActivity != null) currentActivity.javaClass == cls else false
    }

    /**
     * 获取当前activity
     * @return Activity
     */
    fun getCurrentActivity(): Activity? {
        return if (!activityStack.empty()) activityStack.lastElement() else null
    }

    /**
     * 结束栈内Activity
     * @param clz Activity Class<*>
     */
    fun finishActivity(clz: Class<*>) {
        if (!activityStack.empty()) {
            activityStack.forEach {
                if (it.javaClass == clz) {
                    if (!it.isFinishing) {
                        activityStack.remove(it)
                        it.finish()
                        return
                    }
                }
            }
        }
    }

    /**
     * 弹出其他 Activity
     */
    fun popOtherActivity() {
        getCurrentActivity()?.run {
            activityStack.forEach {
                if (this != it) {
                    activityStack.remove(it)
                    if (!it.isFinishing) {
                        it.finish()
                    }
                }
            }
        }
    }

    /**
     * 返回到指定Activity
     * @param clz Class<*> 指定Activity的class
     */
    fun backToSpecifyActivity(clz: Class<*>) {
        activityStack.forEach {
            if (it.javaClass == clz) {
                return
            } else {
                activityStack.pop()
                it.finish()
            }
        }
    }

    /**
     * Activity是否销毁
     * @param context
     * @return Boolean true:销毁 false:未销毁
     */
    fun isActivityDestroy(context: Context): Boolean {
        val activity = findActivity(context)
        return if (activity != null) {
            activity.isDestroyed || activity.isFinishing
        } else true
    }

    /**
     * 查找当前Activity
     * @param context Context
     * @return Activity? 当前Activity
     */
    private fun findActivity(context: Context): Activity? {
        // 怎么判断context是不是Activity
        if (context is Activity) { // 这种方法不够严谨
            return context
        } else if (context is ContextWrapper) {
            return findActivity(context.baseContext)
        }
        return null
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        activityStack.forEach {
            if (!it.isFinishing) {
                it.finish()
            }
        }
        activityStack.clear()
    }
}